package com.ldn.basic.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.DES;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import com.ldn.basic.enums.CachePrefixEnums;


import java.util.Random;

/**
 * @author 李大牛家的牛 (scafel@163.com)
 * @createDate 2024/7/9 - 17:16
 * @description 自定义的一些工具类
 **/
@Slf4j
public class DataUtil {

    public static String getCode(String prefix, String suffix) {
        String time = DateTime.now().toString("yyyyMMddHHmmss");
        Random r = new Random();
        int end = r.nextInt(500) + 10;
        return  prefix + time + suffix + end;
    }

    public static String getCode(String prefix) {
        return getCode(prefix, "");
    }

    public static String getCode() {
        return getCode("");
    }

    private static String myEncrypt(String str, char operation) {
        String ENCRYPT_KEY = "encrypt-";
        DES des = new DES(Mode.CBC, Padding.PKCS5Padding, "enDeSign".getBytes(), "PrivateK".getBytes());
        String res = str;
        if (operation == 'E') {
            if (str.getBytes().length < 8) {
                str = ENCRYPT_KEY + str;
            }
            try {
                res = des.encryptHex(str);
            }catch (Exception ex){
                throw new RuntimeException("加密失败|" + ex.getMessage());
            }
        } else if (operation == 'D'){
            try {
                res = des.decryptStr(str);
                if (res.contains(ENCRYPT_KEY)) {
                    res = res.substring(ENCRYPT_KEY.length());
                }
            }catch (Exception ex) {
                throw new RuntimeException("解密失败|" + ex.getMessage() );
            }
        }
        return res;
    }

    public static String en(String str){
        return myEncrypt(str, 'E');
    }

    public static String de(String str){
        return myEncrypt(str, 'D');
    }

    public static String getCacheKey(CachePrefixEnums enums, String... codes) {
        return enums.getPrefix() + String.join("-", codes) + "-";
    }

    public static <T> T  strToBean(String bean, Class<T> T) {
        try {
            JSONObject obj = JSONUtil.parseObj(bean);  // 将字符串转换为JSONObject对象
            return obj.toBean(T);  // 将JSONObject对象转换为指定类型的对象并返回
        }catch (Exception e){
            log.error("转换失败  转换类:{} 原始内容:{}" , T.toString(), bean);
            return null;
        }
    }

    public static String beanToStr(Object bean) {
        JSONObject json = JSONUtil.parseObj(bean, false);
        return json.toString();
    }

    private static byte environ = 0;

    private static void initEnv() {
        ApplicationContext app = AppSpringContextUtil.getApplicationContext();
        String[] profiles = app.getEnvironment().getActiveProfiles();
        for (String profile : profiles) {
            if ("dev".equals(profile)){
                environ = 0;
                break;
            } else if ("test".equals(profile)){
                environ = 1;
                break;
            } else if ("prod".equals(profile)){
                environ = 2;
                break;
            }
        }
    }

    public static boolean isDev() {
        initEnv();
        return environ == 0;
    }

    public static boolean isTest() {
        initEnv();
        return environ == 1;
    }

    public static boolean isProd() {
        initEnv();
        return environ == 2;
    }
}
